﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using fsw.Common;
using System.Text;

namespace fsw.Web.Card
{
    public partial class List : Web.UI.ManagePage
    {
        protected Model.Admin model_admin;//登录用户名
        protected string _Path;
        protected string _Path_file = string.Empty;
        protected List<Model.DirFile> _List_dirfil = new List<Model.DirFile>();
        protected List<Model.Card> _List_file = new List<Model.Card>();
        protected string _Url = string.Empty;
        protected string _Nav = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            model_admin = GetAdminInfo();
            this._Path = Request.ServerVariables["APPL_PHYSICAL_PATH"];
            this._Path_file = DTKeys.Path_File;
            this._Url = DTRequest.GetQueryString("url");
            this._Nav = GetNav(this._Url);
            if (!DirFileHelper.IsEmptyDirectory(Server.MapPath(this._Path_file + this._Url)))
            {
                string[] _dirfile = null;
                if (!string.IsNullOrEmpty(_Url))
                    _dirfile = DirFileHelper.GetDirectories(Server.MapPath(this._Path_file + _Url));
                else
                    _dirfile = GetCardList().ToArray();
                string[] _file = DirFileHelper.GetFileNames(Server.MapPath(this._Path_file + this._Url));
                _List_dirfil = GetListDirFile(_dirfile);
                _List_file = GetListCard(_file);
            }
        }
        /// <summary>
        /// 文件夹列表
        /// </summary>
        /// <param name="dirfile"></param>
        /// <returns></returns>
        private List<Model.DirFile> GetListDirFile(string[] dirfile)
        {
            List<Model.DirFile> _list = new List<Model.DirFile>();
            for (int i = 0; i < dirfile.Length; i++)
            {
                string _DirFileUrl = dirfile[i].Replace(_Path, "/").Replace(@"\", "/");
                string[] _DirFileUrlArr = _DirFileUrl.Split('_');
                int _DirFilePid = Utils.StringToInt(DirFileHelper.GetFileProject(_DirFileUrlArr[0]));
                string _DirFileProject = new BLL.Project().GetTitle(_DirFilePid);
                string _DirFileUrl2 = _DirFileUrl.Replace(this._Path_file.Substring(1), "");
                string _DirFileName = DirFileHelper.GetFileLastName(dirfile[i]);
                if (string.IsNullOrEmpty(this._Url)) _DirFileName = _DirFileProject;
                if (UserProjectLevel(_DirFilePid))
                {
                    _list.Add(new Model.DirFile
                    {
                        DirFileName = _DirFileName,
                        DirFileUrl = _DirFileUrl2,
                        DirFilePid = _DirFilePid,
                        DirFileProject = _DirFileProject,
                        DirFileTime = DirFileHelper.GetFileTime(dirfile[i])
                    });
                }
            }
            return _list;
        }
        /// <summary>
        /// 图片List
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private List<Model.Card> GetListCard(string[] file)
        {
            List<Model.Card> _list = new List<Model.Card>();
            for (int i = 0; i < file.Length; i++)
            {
                if (Utils.CheckFileImg(file[i]) || Utils.CheckFileMov(file[i]))
                {
                    string _FileUrl = file[i].Replace(_Path, "/").Replace(@"\", "/");
                    string _FileClickUrl = ChineseSpell.MakeSpellCode(_FileUrl, "", SpellOptions.EnableUnicodeLetter).Replace(DTKeys.Path_File_No, DTKeys.Path_File_Upload_No).Replace(" ", "");
                    _list.Add(new Model.Card
                    {
                        FileName = DirFileHelper.GetFileName(file[i]),
                        FileSize = (int)(DirFileHelper.GetFileSize(file[i]) / 1024 + 0.5),
                        FileUrl = Uri.EscapeUriString(_FileUrl),
                        FileClickUrl = _FileClickUrl,
                        FileTime = DirFileHelper.GetFileTime(file[i])
                    });
                }
            }
            return _list;
        }
        /// <summary>
        /// 获取栏目地址
        /// </summary>
        /// <param name="_url"></param>
        /// <returns></returns>
        private string GetNav(string _url)
        {
            string _NavVal = string.Empty;
            if (!string.IsNullOrEmpty(_url))
            {
                StringBuilder sb = new StringBuilder();
                _url = _url.TrimEnd('/');
                string[] _urlarr = _url.Split('/');
                if (_urlarr.Length > 1)
                {
                    string _urlarr1 = _urlarr[1];
                    string[] _urlarr1arr = _urlarr1.Split('_');
                    int _pid = Utils.StringToInt(_urlarr1arr[0]);
                    string _pidname = new BLL.Project().GetTitle(_pid);
                    string _u = "/" + _urlarr1;
                    sb.Append(" > ");
                    sb.AppendFormat("<a href=\"?url=/{0}\">", _urlarr1);
                    sb.Append(_pidname);
                    sb.Append("</a>");
                    for (int i = 2; i < _urlarr.Length; i++)
                    {
                        _u += "/" + _urlarr[i];
                        sb.Append(" > ");
                        sb.AppendFormat("<a href=\"?url={0}\">", _u);
                        sb.Append(_urlarr[i]);
                        sb.Append("</a>");
                    }
                }
                _NavVal = sb.ToString();
            }
            return _NavVal;
        }
        /// <summary>
        /// 排序
        /// </summary>
        /// <returns></returns>
        private List<string> GetCardList()
        {
            List<string> _List = new List<string>();
            Dictionary<int, string> _ListInt = new Dictionary<int, string>();
            if (!DirFileHelper.IsEmptyDirectory(Server.MapPath(this._Path_file)))
            {
                string[] _dirfile = DirFileHelper.GetDirectories(Server.MapPath(this._Path_file));
                for (int i = 0; i < _dirfile.Length; i++)
                {
                    string _DirFileUrl = _dirfile[i].Replace(_Path, "/").Replace(@"\", "/");
                    string[] _DirFileUrlArr = _DirFileUrl.Split('_');
                    int _DirFilePid = Utils.StringToInt(DirFileHelper.GetFileProject(_DirFileUrlArr[0]));
                    int _OrderNo = GetPidOrderNo(_DirFilePid);
                    _ListInt.Add(_OrderNo, _dirfile[i]);
                }
                Dictionary<int, string> dic1desc = _ListInt.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
                foreach (KeyValuePair<int, string> k in dic1desc)
                {
                    _List.Add(k.Value);
                }
            }
            return _List;
        }
        /// <summary>
        /// 所有项目
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        private int GetPidOrderNo(int Id)
        {
            List<Model.Project> _ListPid = new BLL.Project().GetList();
            foreach (Model.Project m in _ListPid)
            {
                if (m.Pid == Id) return m.OrderNo;
            }
            return 1000;
        }
    }
}